<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24" v-if="deptId==101">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable prefix-icon="Search" style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="{ label: 'label', children: 'children' }" :expand-on-click-node="false" :filter-node-method="filterNode"
            ref="deptTreeRef" node-key="id" highlight-current default-expand-all @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="deptId==101?20:24" :xs="24">

        <!-- <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="部门" prop="deptId">
            <el-tree-select v-model="queryParams.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
              class="margin-right" placeholder="请选择部门" check-strictly clearable @change="bindChangeTree($event,'deptId')" />
          </el-form-item>
          <el-form-item label="物品名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入物品名" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form> -->

        <!-- <el-tabs v-model="radioType" class="demo-tabs" @tab-change="bindChangeType"> -->
        <!-- <el-tab-pane label="物品配置" name="1"> -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['coordination:goods:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['coordination:goods:export']">导出</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Menu" @click="handleStatistics">统计</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange" show-overflow-tooltip>
          <el-table-column type="index" width="65" align="center" label="序号" />
          <el-table-column label="部门" align="center" prop="deptName" />
          <el-table-column label="物品名" align="center" prop="name" />
          <el-table-column label="数量" align="center" prop="quantity" />
          <el-table-column label="用途" align="center" prop="purpose" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleRecord(scope.row)" v-hasPermi="['coordination:record:add']">使用登记</el-button>
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['coordination:goods:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['coordination:goods:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination style="height:60px!important;" v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
          @pagination="getList" />

        <!-- </el-tab-pane>
          <el-tab-pane label="物品使用记录" name="2">

            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExportRecord" v-hasPermi="['coordination:goods:export']">导出</el-button>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange" show-overflow-tooltip>
              <el-table-column type="index" width="65" align="center" label="序号" />
              <el-table-column label="领用时间" align="center" prop="takeTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.takeTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="使用人" align="center" prop="userName" />
              <el-table-column label="服务对象" align="center" prop="serviceOb" />
              <el-table-column label="使用情况" align="center" prop="usage" />
              <el-table-column label="印证资料" align="center" prop="proof" width="100">
                <template #default="scope">
                  <image-preview :src="scope.row.proof" :width="50" :height="50" />
                </template>
              </el-table-column>
            </el-table>

            <pagination style="height:60px!important;" v-show="recordTotal>0" :total="recordTotal" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
              @pagination="getListRecord" />

          </el-tab-pane>
        </el-tabs> -->

      </el-col>
    </el-row>

    <!-- 添加或修改便民服务 物品配置对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="goodsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门" prop="deptId">
          <el-tree-select style="width: 100%;" v-model="form.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }"
            value-key="id" class="margin-right" placeholder="请选择部门" check-strictly clearable />
        </el-form-item>
        <el-form-item label="物品名" prop="name">
          <el-input v-model="form.name" placeholder="请输入物品名" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="用途" prop="purpose">
          <el-input v-model="form.purpose" placeholder="请输入用途" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" rows="6" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="使用登记" v-model="dialog" width="1000px" append-to-body>
      <el-form ref="recordRef" :model="formRecord" :rules="rules" label-width="80px">
        <el-form-item label="领用时间" prop="takeTime">
          <el-date-picker clearable style="width: 100%;" v-model="formRecord.takeTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择领用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用人" prop="userId">
          <el-select style="width:100%" v-model="formRecord.userId" placeholder="请选择">
            <el-option v-for="item in userList" :key="item.id" :label="item.userName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="服务对象" prop="serviceOb">
          <el-input v-model="formRecord.serviceOb" placeholder="请输入服务对象" />
        </el-form-item>
        <el-form-item label="使用情况" prop="usage">
          <el-input v-model="formRecord.usage" placeholder="请输入使用情况" />
        </el-form-item>
        <el-form-item label="印证资料" prop="proof">
          <image-upload v-model="formRecord.proof" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formRecord.remark" placeholder="请输入备注" type="textarea" rows="6" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormRecord">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Goods">
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/coordination/goods";
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/coordination/record";
import { deptTreeSelect } from "@/api/system/user";
import { getCAuditorUser } from "@/api/charge/cAuditor";
import { getListCharge } from "@/api/charge/charge";
import { getCKInfo } from '@/utils/auth'
const { proxy } = getCurrentInstance();
const userList = ref([])
const goodsList = ref([]);
const open = ref(false);
const recordTotal = ref(0)
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const radioType = ref("1")
const deptOptions = ref([])
const dialog = ref(false)
const deptId = ref(Number(getCKInfo('deptId')))
const recordList = ref([])
const data = reactive({
  form: { deptId: deptId.value, },
  formRecord: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: deptId.value,
    name: null,
  },
  rules: {
  }
});

const { queryParams, form, rules, formRecord } = toRefs(data);

/** 查询便民服务 物品配置列表 */
function getList() {
  loading.value = true;
  listGoods(queryParams.value).then(response => {
    goodsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询便民服务 物品使用记录列表 */
function getListRecord() {
  loading.value = true;
  listRecord(queryParams.value).then(response => {
    recordList.value = response.rows;
    recordTotal.value = response.total;
    loading.value = false;
  });
}
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
    getUserList()
  });
};
function getUserList() {
  getListCharge({ deptId: deptId.value }).then(response => {
    userList.value = response.rows;
  });
}

function bindChangeType(val) {
  queryParams.value.pageNum = 1;


  if (val == "1") {
    getList()
  } else {
    getListRecord()
  }
}
// 取消按钮
function cancel() {
  open.value = false;
  dialog.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    goodsId: null,
    deptId: deptId.value,
    name: null,
    quantity: null,
    purpose: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("goodsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.goodsId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加便民服务-物品配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _goodsId = row.goodsId || ids.value
  getGoods(_goodsId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改便民服务-物品配置";
  });
}
/** 使用登记 */
function handleRecord(row) {
  formRecord.value = { goodsId: row.goodsId };
  dialog.value = true;
  // reset();
  // const _goodsId = row.goodsId || ids.value
  // getGoods(_goodsId).then(response => {

  // });
}
function handleNodeClick(data, name) {
  queryParams.value.deptId = data.id;
  form.value.deptId = data.id;
  if (radioType.value == '1') {
    getList()
  } else {
    getListRecord()
  }

}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["goodsRef"].validate(valid => {
    if (valid) {
      if (form.value.goodsId != null) {
        updateGoods(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGoods(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}


/** 提交按钮 */
function submitFormRecord() {
  proxy.$refs["recordRef"].validate(valid => {
    if (valid) {
      addRecord(formRecord.value).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        dialog.value = false;
        getListRecord();
      });
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _goodsIds = row.goodsId || ids.value;
  proxy.$modal.confirm('是否确认删除便民服务 物品配置编号为"' + _goodsIds + '"的数据项？').then(function () {
    return delGoods(_goodsIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('coordination/goods/export', {
    ...queryParams.value
  }, `便民服务物品配置_${new Date().getTime()}.xlsx`)
}

/** 导出按钮操作 */
function handleExportRecord() {
  proxy.download('coordination/record/export', {
    ...queryParams.value
  }, `便民服务物品使用_${new Date().getTime()}.xlsx`)
}


getList();
getDeptTree()
</script>
